OpenBuildings GenerativeComponents Help

Sort

Returns a new list comprising all members of the given list, in a sorted sequence.

object[] Sort(object[] list, bool function(object x, object
y, ...) putBefore, ...)

The given function specifies the sort criteria. Given any two members, x and y, that function returns true if x should be put before y in the sorted sequence.

The Sort function does not change the given list. To sort a list directly, call the Sort method on that list, itself.

How to use it

Sort is useful Function is GC Script, particularly when dealing with vast numbers of object where specific sorting criteria are needed.

a) One line format:

double[] sortedNumbers = Sort(numbers, function(x, y) {
return x < y; });

b) Multi-line format:

function putBefore(x, y)
{
	return x < y;
}
double[] sortedNumbers = Sort(numbers, putBefore);

The sort function goes through the list making comparisons between pairs of values according to the supplied sort criteria. The boolean putBefore function determines whether a number be put before another in the list. If this function returns true, it means that, when the list is sorted, x should be placed before y.

Examples

A) Sort numbers in ascending order

double[] numbers = {3, 5, 1, 6, -6, 0, 1};
double[] sortedNumbers = Sort(numbers, function(x, y) {
return x < y; });
//sortedNumbers =  {-6, 0, 1, 1, 3, 5}

B) Sort sequentially by Z, X, Y

bool function sortByZXY(Point pt1, Point pt2)
{
return pt1.Z < pt2.Z
	|| (pt1.Z == pt2.Z && pt1.X <  pt2.X)
	|| (pt1.Z == pt2.Z && pt1.X == pt2.X 
	&& pt1.Y < pt2.Y);
}
Point sortedPoints = Sort(curvePoint, sortByZXY);

Sorting sequentially by Z and, then by X and lastly if Z and X are equal then by Y.